<?php
/**
 * Created by PhpStorm.
 * User: 悟能科技
 * Date: 2018/5/22
 * Time: 17:27
 */

namespace app\merchant\controller;


use app\common\controller\AdminBase;
use app\common\model\CardFans;
use app\common\model\CardGeneral;
use app\common\model\CardUserBonus;
use app\common\model\CardUserFee;
use app\common\model\CardUserGet;
use app\common\model\CardUserMarkLog;
use app\common\model\CardUserUpdate;
use app\common\model\Paylog;
use think\Controller;
use think\Request;
use think\Session;

class Cardusers extends Controller
{

    //获取会员基本信息
    /*
     * id
     */
    public function getcarduserinfo(){
//        if (Request()->isAjax()){
            $arr = request()->post();
            $CardUserGet = new CardUserGet();
            $user = [];
            $user['user'] = $CardUserGet->where(array('id'=>$arr['id']))->field('gs_mid,bonus,email,balance,mid,top_mid,card_id,sex,code,outerstr,nickname,name,phone,have,address,fire,openid,birthday,create_time,status')->find();
            $user['user']['merchant'] = $user['user']['gs_mid'] == 0?getmerchant($user['user']['mid']):getmerchant($user['user']['gs_mid']);
            $user['user']['zodiac'] = $this->getuserzodiac($user['user']['birthday']);
            $user['user']['level'] = getlivename($user['user']['card_id'],$user['user']['bonus']);
            $user['user']['age'] = $this->userage($user['user']['birthday']);
            $user['user']['is_follow'] = getfollowstatu($user['user']['openid']);
            $user['user']['sharename'] = getsharename($user['user']['outerstr']);
            $user['user']['sex'] =$user['user']['sex'] == 1?'男':'女';
            $user['cards'] = $this->getcardsnum($user['user']);

            $arr['openid'] = $user['user']['openid'];
            $user['wxinfo'] = $this->getwxfansinfo($arr);
            if ($user){
                $rarr = ['code'=>0,'message'=>'SUCCESS','data'=>$user];
            }else{
                $rarr = ['code'=>9001,'message'=>'未找到'];
            }
            return json($rarr);
//        }
    }


    //修改会员  remark
    public function getcarduseredit(){
        if (Request()->isAjax()) {
            $arr = request()->post();
            $data['birthday'] = $arr['birthday'];
            $data['email'] = $arr['email'];
            $data['name'] = $arr['name'];
            $data['nickname'] = $arr['nickname'];
            $data['phone'] = $arr['phone'];
            $data['gs_mid'] = $arr['gs_mid_new'];
            $data['status'] = !empty($arr['status'])?$arr['status']:1;
            $data['sex'] = $arr['sex'];
            $data['remark'] = $arr['remark'];
            $data['update_time'] = time();
            $map['code'] = $arr['code'];
            $map['openid'] = $arr['openid'];
            $CardUserGet = new CardUserGet();
            $res = $CardUserGet->allowField(true)->save($data,$map);
            if ($res){
                $rs = $this->rec_mark($map);
                $rarr = ['code'=>0,'message'=>'修改成功','re'=>$rs,'data'=>$data];

            }else{
                $rarr = ['code'=>9001,'message'=>'修改失败','sql'=>$CardUserGet->getLastSql(),'arr'=>$arr];
            }
            return json($rarr);
        }
    }

    //备注记录
    private function rec_mark($map){
        $CardUserGet = new CardUserGet();
        $user = $CardUserGet->where($map)->field('code,card_id,mid,remark,openid')->find();
        $CardUserMarkLog = new CardUserMarkLog();
        $data['code'] = $user['code'];
        $data['card_id'] = $user['card_id'];
        $data['mid'] = $user['mid'];
        $data['con_username'] = Session::get('user.nickname');
        $data['remark'] = $user['remark'];
        $data['openid'] = $user['openid'];
        $data['create_time'] = time();
        $s = $CardUserMarkLog->allowField(true)->save($data);
        return $s;
    }

    //备注记录
    public function getremarklist(){
        if (Request()->isAjax()) {
            $arr = request()->post();
            $CardUserMarkLog = new CardUserMarkLog();
            $page=($arr['page']-1)*10;
            $map['openid'] = $arr['openid'];
            $map['code'] = $arr['code'];
            $list = $CardUserMarkLog->where($map)->limit($page,10)->order('create_time','desc')->select();
            $count = $CardUserMarkLog->where($map)->count();
            if ($list){
                $data['code'] = 0;
                $data['message'] = 'SUCCESS';
                $data['data'] = $list;
                $data['conut'] = $count;
                $data['page'] = $arr['page'];
                $data['pagenum'] = ceil($count/10);
            }else{
                $data['code'] = 9001;
                $data['message'] = 'ERROR';
                $data['data'] = '';
            }
            return json($data);
        }
    }

    //获取消费信息
    /*
     * openid  code  top_mid
     */
    public function getcarduserxfinfo(){
        if (Request()->isAjax()) {
            $arr = request()->post();
            $CardUserBonus = new CardUserBonus();
            $CardUserFee = new CardUserFee();
            $Paylog = new Paylog();
            $mapspay['pay_statu'] = 1;
            $mapspay['pay_fs'] = ['in',[0,2]];
            $mapspay['openid'] = $arr['openid'];
            $xfcount['wx_xf_num'] = $Paylog->where($mapspay)->count('id');
            //消费统计
            $mapdel['openid'] = $arr['openid'];
            $mapdel['code'] = $arr['code'];
            $mapdel['type'] = 2;
            //减少END
            $map['openid'] = $arr['openid'];
            $map['code'] = $arr['code'];
            $map['type'] = ['neq',2];
            //增加END
            $pay_balance = $CardUserFee->where($map)->sum('num');   //应付金额
            $xfcount['add_balance'] = $CardUserFee->where($map)->sum('num');   //充值金额
            $xfcount['del_balance'] = $CardUserFee->where($mapdel)->sum('num');   //消费金额
            $xfcount['add_bonus'] = $CardUserBonus->where($map)->sum('num');   //累计获得积分
            $xfcount['del_bonus'] = $CardUserBonus->where($mapdel)->sum('num');   //累计使用积分
            //金额 积分END
            $xfcount['xf_nume'] = $CardUserFee->where($mapdel)->count();   //消费笔数
            $map['type'] = 1;
            $xfcount['cz_nume'] = $CardUserFee->where($map)->count();   //充值笔数
            unset($map['type']);
            //充值笔数END
            $map['top_mid'] = $arr['top_mid'];
            $maps = $map;
            unset($maps['code']);
            $xfcount['get_cards'] = $this->getcardsnum($maps);;   //累计获得卡券
            //消费统计END
            //最近消费
            unset($map['top_mid']);
            $xfjl = $CardUserFee->where($mapdel)->order('create_time desc')->limit(1)->select();
            if ($xfjl){
                $zjxf = $xfjl[0];
            }else{
                $zjxf = ['mid'=>0,'num'=>0,'create_time'=>0];
            }
            $lately['merchant'] = getmerchant($zjxf['mid']);        //最近消费门店
            $lately['pay_balance'] = $zjxf['num'];        //最近消费金额\
            $lately['pay_time'] = $zjxf['create_time'] == 0?'无':$zjxf['create_time'];        //最近消费时间    $zjxf['create_time'] == 0?'无':
            //最近消费END
            //平均消费
            $average['del_balance'] = $xfcount['del_balance'] == 0?0:$xfcount['del_balance']/ $xfcount['xf_nume'];        //平均消费
            $average['add_balance'] = $xfcount['add_balance'] == 0?0:$xfcount['add_balance']/ $xfcount['cz_nume'];        //平均充值
            $xfcount['del_balance'] = $xfcount['del_balance']==0?$pay_balance:
            $average['discount'] = $pay_balance == 0?0:$pay_balance/$xfcount['del_balance'];                   //平均折扣
            //平均消费END
            //返回值
            $data['code'] = 0;
            $data['message'] = 'SUCCESS';
            $data['xfcount'] = $xfcount;        //消费统计
            $data['lately'] = $lately;          //最近消费
            $data['average'] = $average;          //平均消费
            //返回值END
            return json($data);
        }
    }

    //获取消费记录
    /*
     * openid  code
     */
    public function getcarduserxflist(){
        if (Request()->isAjax()) {
            $arr = request()->post();
            $CardUserFee = new CardUserFee();
            $page=($arr['page']-1)*10;
            $map['openid'] = $arr['openid'];
            $map['code'] = $arr['code'];
            $map['type'] = 2;
            $list = [];
            $list = $CardUserFee->where($map)->limit($page,10)->order('create_time','desc')->select();
            foreach ($list as $k=>$v){
                $list[$k]['merchant'] = getmachname($v['mid']);
            }
            $count = $CardUserFee->where($map)->count();
            if ($list){
                $data['code'] = 0;
                $data['message'] = 'SUCCESS';
                $data['data'] = $list;
                $data['conut'] = $count;
                $data['page'] = $arr['page'];
                $data['pagenum'] = ceil($count/10);
            }else{
                $data['code'] = 9001;
                $data['message'] = 'ERROR';
                $data['data'] = '';
            }
            return json($data);
        }
    }

    //获取微信消费记录
    /*
     * openid  code
     */
    public function getcardwxxflist(){
        if (Request()->isAjax()) {
            $arr = request()->post();
            $Paylog = new Paylog();
            $page=($arr['page']-1)*10;
            $map['openid'] = $arr['openid'];
            $map['pay_statu'] = 1;
            $map['pay_fs'] = ['in',[0,2]];
            $list = [];
            $list = $Paylog->where($map)->limit($page,10)->field('mid,pay_cate,pay_fs,pay_statu,price,add_time')->order('add_time','desc')->select();
            $sql = $Paylog->getLastSql();
            foreach ($list as $k=>$v){
                $list[$k] = $v;
                $list[$k]['pay_fs'] = getpayfs($v['pay_fs']);
                $list[$k]['merchant'] = getmachname($v['mid']);
                $list[$k]['add_time'] = date('Y-m-d h:i:s',$v['add_time']);
                $list[$k]['pay_statu'] = getpaystatus($v['pay_statu']);
            }
            $count = $Paylog->where($map)->count();
            if ($list){
                $data['code'] = 0;
                $data['message'] = 'SUCCESS';
                $data['data'] = $list;
                $data['conut'] = $count;
                $data['page'] = $arr['page'];
                $data['pagenum'] = ceil($count/10);
            }else{
                $data['code'] = 9001;
                $data['message'] = 'ERROR';
                $data['data'] = '';
            }
            return json($data);
        }
    }

    //获取充值记录
    /*
     * openid  code
     */
    public function getcarduserczlist(){
        if (Request()->isAjax()) {
            $arr = request()->post();

            $CardUserFee = new CardUserFee();
            $page=($arr['page']-1)*10;
            $map['openid'] = $arr['openid'];
            $map['code'] = $arr['code'];
            $map['type'] = 1;
            $list = $CardUserFee->where($map)->limit($page,10)->order('create_time','desc')->select();
            foreach ($list as $k=>$v){
                $list[$k]['merchant'] = getmachname($v['mid']);
                $list[$k]['pay_fs'] = getpayfs($v['pay_cate']);
            }
            $count = $CardUserFee->where($map)->count();
            if ($list){
                $data['code'] = 0;
                $data['message'] = 'SUCCESS';
                $data['data'] = $list;
                $data['conut'] = $count;
                $data['page'] = $arr['page'];
                $data['pagenum'] = ceil($count/10);
            }else{
                $data['code'] = 9001;
                $data['message'] = 'ERROR';
                $data['data'] = '';
            }
            return json($data);
        }
    }

    //获取积分记录
    /*
     * openid  code
     */
    public function getcarduserbonuslist(){
        if (Request()->isAjax()) {
            $arr = request()->post();
            $CardUserBonus = new CardUserBonus();
            $page=($arr['page']-1)*10;
            $map['openid'] = $arr['openid'];
            $map['code'] = $arr['code'];
            $map['num'] = ['neq',0];
            $list = $CardUserBonus->where($map)->limit($page,10)->order('create_time','desc')->select();
            foreach ($list as $k=>$v){
                $list[$k]['cate'] = getcate($v['cate']);
//                $list[$k]['pay_fs'] = getpayfs($v['pay_cate']);
            }
            $count = $CardUserBonus->where($map)->count();
            if ($list){
                $data['code'] = 0;
                $data['message'] = 'SUCCESS';
                $data['data'] = $list;
                $data['conut'] = $count;
                $data['page'] = $arr['page'];
                $data['pagenum'] = ceil($count/10);
            }else{
                $data['code'] = 9001;
                $data['message'] = 'ERROR';
                $data['data'] = '';
            }
            return json($data);
        }
    }

    //获取卡券记录
    /*
     * openid  top_mid
     */
    public function getcardusercardslist(){
        if (Request()->isAjax()){
            $arr = request()->post();
            $CardUserGet = new CardUserGet();
            $page=($arr['page']-1)*10;
            $map['openid'] = $arr['openid'];
            $map['top_mid'] = $arr['top_mid'];
            $map['card_type'] = ['neq','MEMBER_CARD'];
            $list = $CardUserGet->where($map)->limit($page,10)->order('create_time','desc')->select();
            foreach ($list as $k=>$v){
                $list[$k]['card_type'] = getcardtype($v['card_type']);
                $list[$k]['title'] = cardgetinfo($v['card_id'],1,1);
                $list[$k]['merchant'] = getmachname($v['mid']);
                $list[$k]['card_statu'] = $v['statu']==0?'未使用':'已使用';
            }
            $count = $CardUserGet->where($map)->count();
            if ($list){
                $data['code'] = 0;
                $data['message'] = 'SUCCESS';
                $data['data'] = $list;
                $data['conut'] = $count;
                $data['page'] = $arr['page'];
                $data['pagenum'] = ceil($count/10);

            }else{
                $data['code'] = 9001;
                $data['message'] = 'ERROR';
                $data['data'] = '';
            }
            return json($data);
        }

    }



    //获取所属微信信息
    /*
     * OPENID
     */
    private function getwxfansinfo($arr){
        $CardFans = new CardFans();
        $info = $CardFans->where(['openid'=>$arr['openid']])->field('head_url,sex,city,province,country')->find();
        $info['sex'] = $info['sex'] == 1?'男':'女';
        return $info;
//        if ($info){
//            $data['code'] = 0;
//            $data['message'] = 'SUCCESS';
//            $data['data'] = $info;
//        }else{
//            $data['code'] = 9001;
//            $data['message'] = 'ERROR';
//            $data['data'] = '';
//        }
    }

    //获取卡券总数
    private function getcardsnum($arr){
//        return $arr;
        $CardUserGet = new CardUserGet();
        $map['openid'] = $arr['openid'];
        $map['top_mid'] = $arr['top_mid'];
        $map['card_type'] = ['neq','MEMBER_CARD'];
        $num = $CardUserGet->where($map)->count();   //累计获得卡券
        return $num;
    }

    //获取生日所属星座
    private function getuserzodiac($birthday){
        if (!$birthday){return 'null';}
        $month = intval(substr($birthday, 5, 2));
        $day = intval(substr($birthday, 8, 2));
        if ($month < 1 || $month > 12 || $day < 1 || $day > 31)
        {
            return NULL;
        }
        $signs = array(
            array('20' => '水瓶座'),
            array('19' => '双鱼座'),
            array('21' => '白羊座'),
            array('20' => '金牛座'),
            array('21' => '双子座'),
            array('22' => '巨蟹座'),
            array('23' => '狮子座'),
            array('23' => '处女座'),
            array('23' => '天秤座'),
            array('24' => '天蝎座'),
            array('22' => '射手座'),
            array('22' => '摩羯座')
        );
        list($start, $name) = each($signs[$month - 1]);
        if ($day < $start)
        {
            list($start, $name) = each($signs[($month - 2 < 0) ? 11 : $month - 2]);
        }

        return $name;
    }

    //计算年龄
    private function userage($birthday){
        if (!$birthday){return 'null';}
        list($year,$month,$day) = explode("-",$birthday);
        $year_diff = date("Y") - $year;
        $month_diff = date("m") - $month;
        $day_diff  = date("d") - $day;
        if ($day_diff < 0 || $month_diff < 0)
            $year_diff--;
        return $year_diff;
    }

}